<?php

class DB_Connector {

    private $db;

    private $host;
    private $user;
    private $pass;
    private $db_name;
    private static $sqlErrorLogFileName = 'sql.errors.log';


    public static $use_custom_db = false;
    public static $custom_db_name = '';

    public function __construct($host='dd11820.kasserver.com',$user='d0145395',$pass='aCBbFfSfPUNVd8oV',$db='d0145395') {
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        if(!self::$use_custom_db)
            $this->db_name = $db;
        else
            $this->db_name = self::$custom_db_name;

    }

    public function getConn() {
        $this->connect();
        return $this->db;
    }

    public function connect() {
        if(isset($this->db))
            return;
        try {
            $this->db = mysql_connect($this->host, $this->user, $this->pass);
            if($this->db === false){
                //Log::write_error('Could not connect to database  '  . $this->db_name . ' on server ' .  $this->host);
                die();
            }
            mysql_select_db($this->db_name);
            mysql_query("set names 'utf8'");
        } catch(Exception $e) {
            echo $e->getFile().' '.$e->getLine().' '.$e->getMessage().'</br>';
            echo $e->getTraceAsString().'</br>';
        }
    }

    public function disconnect() {

    }

    public function __destruct() {
        $this->disconnect();
    }

    public function reconnect() {
        $this->disconnect();
        $this->connect();
    }

    public function api_escape($string) {
       return addslashes($string);
    }
    
    public function select($query){
        $this->connect();
        $ret = array();
        $counter = 0;
        try {
            $result = mysql_query($query,$this->db);
            if(mysql_errno($this->db) != 0) {
                $errorMessage = mysql_error($this->db);

                $errorMessage = "\nQuery:$query\nError:$errorMessage";
                //Wayido::setLastError($errorMessage);
                //Log::writeToFile($errorMessage, self::$sqlErrorLogFileName);
                return false;
            }
           
            if($result)
                while ($row = mysql_fetch_assoc($result)) {
                    $ret[] = $row;
                    unset($row);
                }
        } catch(Exception $e) {
            echo $e->getFile().' '.$e->getLine().' '.$e->getMessage().'</br>';
            echo $e->getTraceAsString().'</br>';
        }
        return $ret;
    }

    public function selectColumn($query, $column, $flag=false){
        $this->connect();
        //    $query = $this->api_escape($query);
        $ret = array();
        $counter = 0;
        try {
            $result = mysql_query($query,$this->db);
            if(mysql_errno($this->db) != 0) {
                $errorMessage = mysql_error($this->db);

                $errorMessage = "\nQuery:$query\nError:$errorMessage";
                //Wayido::setLastError($errorMessage);
                //Log::writeToFile($errorMessage, self::$sqlErrorLogFileName);
                return false;
            }

            if($result)
                while ($row = mysql_fetch_assoc($result)) {
                    if ($flag) {
                        $ret[$row[$column]] = $row[$column];
                    } else {
                        $ret[] = $row[$column];
                    }
                    
                    unset($row);
                }
        } catch(Exception $e) {
            echo $e->getFile().' '.$e->getLine().' '.$e->getMessage().'</br>';
            echo $e->getTraceAsString().'</br>';
        }
        return $ret;
    }

    public function update($query) {
        $this->connect();
        $ret = array();
        $afectedRows = array();
        $lastInsertId = array();

        // do splitintg by semicolon,m but we have to watch about semicolon
        // inside a SQL string , e.g ' sql stri;ng'
        $query = ';' . $query;
        //$queries = explode(';',trim($query));
        $inSqlString = false;
        $queries= array();
        $j=0;
        $query_num=0;
        for($i=0;$i<strlen($query);$i++) {

            $curr = $query[$i];
            if($i>0)
                $before = $query[$i-1];
            else  $before  = '';

            if($curr=="'" && $before!='\\')
                $inSqlString = !$inSqlString;

            if($curr==';' && !$inSqlString) {
                $queries[$query_num++]='';
                $j=0;
            }
            else {
                $queries[$query_num-1] .= $curr;
            }
        }
        
        foreach($queries as $_query) {
            if(trim($_query)=="") continue;
            $_query .= ';';
			
            $result = mysql_query($_query,$this->db);
            $afectedRows[] = mysql_affected_rows($this->db);
            $lastInsertId[] = mysql_insert_id($this->db);  

            if(mysql_errno($this->db) != 0) {         
                $errorMessage = mysql_error($this->db);
                $errorMessage = "\nQuery:$query\nError:$errorMessage";
                //Wayido::setLastError($errorMessage);
                //Log::writeToFile($errorMessage, self::$sqlErrorLogFileName);
                return false;
            }           
            if($result===false)
                return false;
      
                
        }


        $ret['affected_rows'] = $afectedRows;
        $ret['last_insert_id'] = $lastInsertId;
        return $ret;
    }
}

?>
